package nl.hr.minor.mobilelife.j1213.jlr.flow;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;

public class Line {

	public static float LINE_STROKE_WIDTH = 0;
	public HostBlock[] hosts;
	private ArrayList<Block> points;
	private Path path;
	private boolean hostsConnected;
	public int position;
	private Paint linePaint;

	public Line(HostBlock[] hosts) {
		this(hosts, null, false);
	}

	public Line(HostBlock[] hosts, ArrayList<Block> blocks, boolean hostsConnected) {
		this.hosts = hosts.clone();
		this.hostsConnected = hostsConnected;

		points = blocks == null ? new ArrayList<Block>() : blocks;

		linePaint = new Paint();
		linePaint.setStyle(Paint.Style.STROKE);
		linePaint.setStrokeWidth(LINE_STROKE_WIDTH);
		linePaint.setColor(getColor());
		linePaint.setStrokeJoin(Paint.Join.ROUND);

		calculatePath();
	}

	public void draw(Canvas canvas) {
		if(points.size() != 0) {
			canvas.drawPath(path, linePaint);
		}
	}

	public void addBlock(Block block) {
		Log.i("Line", "Adding block " + block);
		linePaint.setStrokeWidth(LINE_STROKE_WIDTH);
		points.add(block);
		calculatePath();
	}

	public void reset() {
		points.clear();
		hostsConnected = false;
		setStartHostBlock(null);
		calculatePath();
	}

	private void calculatePath() {
		path = new Path();
		if(hasStartHost()) {
			path.moveTo(getStartHost().centerX(), getStartHost().centerY());

			for(Block b : points)
				path.lineTo(b.centerX(), b.centerY());

			if(hostsConnected) 
				path.lineTo(getEndHost().centerX(), getEndHost().centerY());
		}
	}

	public int getColor() {
		return hosts[0].hostPaint.getColor();
	}

	public boolean hostsConnected(){
		return this.hostsConnected;
	}

	public void setHostsConnected(boolean setConnected) {
		this.hostsConnected = setConnected;
		calculatePath();
	}

	public boolean hasStartHost() {
		return !(hosts[0].isStartHost == false && hosts[1].isStartHost == false);
	}

	public HostBlock getStartHost() {
		if(hasStartHost())
			return hosts[0].isStartHost ? hosts[0] : hosts[1];
			else
				return null;	
	}

	public HostBlock getEndHost() {
		if(hasStartHost())
			return hosts[0].isStartHost ? hosts[1] : hosts[0];
			else
				return null;
	}

	public boolean containsBlock(Block block) {
		if(hosts[0].equals(block) || hosts[1].equals(block))
			return true;
		else
			return points.contains(block);
	}

	public void setStartHostBlock(HostBlock block) {
		if(block == null){
			hosts[0].isStartHost = false;
			hosts[1].isStartHost = false;
		} else if(block.equals(hosts[0])) {
			hosts[0].isStartHost = true;
			hosts[1].isStartHost = false;
		} else if(block.equals(hosts[1])) {
			hosts[0].isStartHost = false;
			hosts[1].isStartHost = true;
		}
	}

	public int getCoveredBlocks(boolean includingHostblocks) {
		return points.size() + (includingHostblocks ? 2 : 0);
	}
}
